import { useEffect, useState } from "react"


export const useResHook = (request, params) => {
  let [data, setData] = useState([])
  let [loading, setLoading] = useState(false)
  let [total, setTotal] = useState(10)  // 数据总条数

  // 创建刷新函数
  let [rsh, setRsh] = useState(false)
  let refresh = () => {
    setRsh(!rsh)
    // console.log("刷新");
  }

  useEffect(() => {
    setLoading(true)
    request(params).then((res) => {
      setData(res.data)
      setTotal(res.total)
    }).finally(() => {
      // 不管成功与否都会执行
      setLoading(false)
    })
  }, [rsh, params])

  return { data, loading, total, refresh }
}